package com.happycat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.happycat.base.R;
import com.happycat.base.context.BaseContextHandler;
import com.happycat.base.exception.RestApiException;
import com.happycat.mapper.social.SocialVoiceRoomMapper;
import com.happycat.mapper.social.SocialVoiceRoomMemberMapper;
import com.happycat.mapper.social.SocialVoiceRoomWheatMapper;
import com.happycat.social.entity.SocialVoiceRoom;
import com.happycat.social.entity.SocialVoiceRoomMember;
import com.happycat.social.entity.SocialVoiceRoomWheat;
import com.happycat.social.service.ISocialVoiceRoomPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 房间权限处理
 * @Author shenbin
 * @CreatedDate 2020-09-11
 * @Modified By:
 */
@Service
public class SocialVoiceRoomPermissionServiceImpl implements ISocialVoiceRoomPermissionService {

    @Autowired
    private SocialVoiceRoomMapper roomMapper;
    @Autowired
    private SocialVoiceRoomMemberMapper memberMapper;
    @Autowired
    private SocialVoiceRoomWheatMapper wheatMapper;

    @Override
    public boolean hasRoomCreatorPermission(long svrId) {
        SocialVoiceRoom room = roomMapper.selectById(svrId);
        if(room==null){
            throw new RestApiException(R.fail("房间不存在"));
        }
        //校验是否房主
        if(BaseContextHandler.getUserId().compareTo(room.getCreateAccountId())==0){
            return true;
        }
        return false;
    }

    @Override
    public boolean hasRoomManagerPermission(long svrId) {
        if(hasRoomCreatorPermission(svrId)){
            return true;
        }
        if(isRoomManager(svrId)){
            return true;
        }
        return false;
    }

    @Override
    public boolean hasRoomHostessPermission(long svrId) {
        if(hasRoomManagerPermission(svrId)){
            return true;
        }
        if(isRoomHostess(svrId)){
            return true;
        }
        return false;
    }

    @Override
    public boolean isRoomManager(long svrId) {
        //校验是否管理员
        SocialVoiceRoomMember ws = new SocialVoiceRoomMember();
        ws.setSvrId(svrId);
        ws.setType(SocialVoiceRoomMember.MANAGER_MEMBER);
        ws.setAccountId(BaseContextHandler.getUserId());
        int count = memberMapper.selectCount(new QueryWrapper<>(ws));
        if(count >= 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean isRoomHostess(long svrId) {
        //校验是否主持人
        SocialVoiceRoomWheat ws = new SocialVoiceRoomWheat();
        ws.setSvrId(svrId);
        ws.setHostess("1");
        ws.setAccountId(BaseContextHandler.getUserId());
        int count = wheatMapper.selectCount(new QueryWrapper<>(ws));
        if(count >= 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean hasRoomCreatorPermission(SocialVoiceRoom room) {
        //校验是否房主
        if(BaseContextHandler.getUserId().compareTo(room.getCreateAccountId())==0){
            return true;
        }
        return false;
    }

    @Override
    public boolean hasRoomManagerPermission(SocialVoiceRoom room) {
        if(hasRoomCreatorPermission(room)){
            return true;
        }
        if(isRoomManager(room.getId())){
            return true;
        }
        return false;
    }

    @Override
    public boolean hasRoomHostessPermission(SocialVoiceRoom room) {
        if(hasRoomManagerPermission(room)){
            return true;
        }
        if(isRoomHostess(room.getId())){
            return true;
        }
        return false;
    }
}
